home *** CD-ROM | disk | FTP | other *** search
- /*
- File: OTGetDefaultEthernetAddress.c
-
- Contains: Demo of accessing OT from CodeWarrior 68K code resource.
-
- Written by: Quinn "The Eskimo!"
-
- Copyright: © 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- */
-
- #include <OpenTransport.h>
- #include <OpenTptLinks.h>
- #include <A4Stuff.h>
- #include <HyperXCMD.h>
- #include <TextUtils.h>
- #include <Strings.h>
- #include <string.h>
- #include <stdio.h>
-
- // Prototypes
-
- static void TrueMain(XCmdPtr xpb);
- pascal void main(XCmdPtr xpb);
-
- // Evil pseudo-LibraryManager stuff
-
- typedef long GlobalWorld;
-
- GlobalWorld GetCurrentGlobalWorld(void):__D0
- = {0x200D}; /* move.l A5,D0 */
-
- void SynchGlobalWorldFromA4(void)
- = {0x2A4C}; /* move.l A4,A5 */
-
- void SynchA4FromGlobalWorld(void)
- = {0x284D}; /* move.l A5,A4 */
-
- GlobalWorld SetCurrentGlobalWorld(GlobalWorld:__A0):__D0
- = {0x200D, /* move.l A5,D0 */
- 0x2A48}; /* move.l A0,A5 */
-
- // Standard XCMD main entry point
-
- pascal void main(XCmdPtr xpb)
- {
- long old_A4;
- GlobalWorld old_world;
-
- // Debugger();
- old_world = GetCurrentGlobalWorld();
-
- old_A4 = SetCurrentA4();
- SynchGlobalWorldFromA4();
-
- TrueMain(xpb);
-
- (void) SetCurrentGlobalWorld(old_world);
- (void) SetA4(old_A4);
-
- // Debugger();
- }
-
- // Generic OT stuff
-
- typedef struct {
- unsigned char bytes[6];
- } EnetAddress, *EnetAddressPtr;
-
- typedef struct T8022Address T8022Address;
-
- static T8022Address simple_addr = {
- AF_8022, // OTAddressType for 802 provider
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // hardware address, use zeros for bind requests
- 0x8888, // SAP / protocol type, value > 1500 implies ethernet protocol with no SNAP
- {0x00,0x00,0x00,0x00,0x00}
- };
-
- static OSStatus GetDefaultEthernetAddress(EnetAddressPtr def_enet_addr)
- {
- EndpointRef ep;
- OSStatus err;
- TBind bind_request;
- TBind bind_result;
- T8022Address theReturnAddr;
-
- ep = OTOpenEndpoint(OTCreateConfiguration(kEnetName), 0, 0, &err);
- if (err == kOTNoError) {
- // finish bind request
- bind_request.addr.buf = (UInt8 *) &simple_addr;
- bind_request.addr.len = k8022BasicAddressLength; // family type + Ethernet + type field
- bind_request.addr.maxlen = 0;
- bind_request.qlen = 0;
- // setup bind result
- bind_result.addr.buf = (UInt8 *) &theReturnAddr;
- bind_result.addr.len = 0;
- bind_result.addr.maxlen = sizeof(theReturnAddr);
- bind_result.qlen = 0;
- // call bind
- err = OTBind(ep, &bind_request, &bind_result);
- if (err == noErr) {
- BlockMove((Ptr) theReturnAddr.fHWAddr, (Ptr) def_enet_addr->bytes, 6);
- // clean up
- err = OTUnbind(ep);
- }
- err = OTCloseProvider(ep);
- }
- return (err);
- }
-
- static void TrueMain(XCmdPtr xpb)
- // Extra procedure wrapper to avoid horrible register caching problem
- // whereby the value of result was held in register A3 which was
- // setup from an A5 relative offset before our A5 had been set up
- // by InitCodeResource. Urgh!
- {
- OSStatus err;
- EnetAddress def_enet_addr;
- char result[256];
-
- err = InitOpenTransport();
- if (err == noErr) {
- err = GetDefaultEthernetAddress(&def_enet_addr);
- if (err == noErr) {
- sprintf(result, "%02X:%02X:%02X:%02X:%02X:%02X",
- def_enet_addr.bytes[0],
- def_enet_addr.bytes[1],
- def_enet_addr.bytes[2],
- def_enet_addr.bytes[3],
- def_enet_addr.bytes[4],
- def_enet_addr.bytes[5]
- );
- };
-
- CloseOpenTransport();
- };
-
- if (err != noErr) {
- strcpy(result, "Error");
- };
-
- (void) PtrToHand((Ptr) result, &xpb->returnValue, strlen(result) + 1);
- }
-